﻿<!DOCTYPE html>
<html>
<head>
    <title>栏目助手</title>
    $css()
    <style type="text/css">
        .panel{ margin: 1em;padding: 1em;background: #FFF;border: solid 1px #EEE;}
        table{ background: #FFF;}
        table .panel{ background: #F8F8F8;}
        .ctrl-panel{ text-align: left;padding: 1em 1em;border-top: solid 1px #EEE;}
        .ctrl-panel #tip-msg{ padding-left: 2em;color: #ff0000;}
        .panel .tit {
            font-weight: bold;
            font-size: 1.2em;
            color: #F00;
            margin-bottom: 1em;
        }
        .note{ padding: 2em;border-bottom: solid 1px #E0E0E0;box-shadow: 0 1px 0 #FFF;}
    </style>
</head>
<body>


<div class="note"><span>使用此工具，可以将其他站点的栏目拷贝到当前站点,</span></div>
 <div>
        <table cellpadding="0" cellspacing="0" width="100%">
            <tr>
                <td width="50%" valign="top">
                    <div class="panel">

                        <div class="tit">
                            源栏目：
                            <select id="site_selector" class="ui-box">
                                <option value="">选择站点</option>
                                ${site_opt}
                            </select>
                        </div>

                        <div class="true" id="from_tree"></div>
                    </div>
                </td>
                <td valign="top">
                    <div class="panel">
                        <div class="tit">目标栏目</div>
                        <div class="hidden" style="padding: 20px 20px 0 20px">
                            <input type="button" class="btn" value="新建栏目" id="btn_create" />&nbsp;
                            <input type="button" class="btn" value="收起栏目" id="btn_collage" />&nbsp;
                            <input type="button" class="btn" value="栏目助手" id="btn_utility" />
                        </div>
                        <div class="tree" id="to_tree"></div>
                    </div>
                </td>
            </tr>
        </table>
</div>


<div class="ctrl-panel">

    <div>
        <input type="checkbox" id="ck_with_child" checked="checked" /><label for="ck_with_child">包括拷贝子栏目</label>
        <input type="checkbox" id="ck_with_extend" checked="checked" /><label for="ck_with_extend">包括扩展属性</label>
        <input type="checkbox" id="ck_with_templateBind" checked="checked" /><label for="ck_with_templateBind">包含模版绑定</label>
    </div>

    <br/>
    <span class="ui-button w150" id="btn_clone">
        <span class="button-inner">
            <span class="button-txt">拷贝栏目</span>
            <a href="javascript:;"></a>
        </span>
    </span>

    <span id="tip-msg">还未选择栏目</span>
</div>

$js()
<script type="text/javascript">
    var siteId = ${target_site};
    var sourceSiteId = 0;
    var fromTreePanel = jr.$('from_tree');
    var toTreePanel = jr.$('to_tree');
    var tipMsg = jr.$('tip-msg');
    var fromNode = {};
    var toNode = {};

   jr.$('site_selector').onchange = function() {
        var sourceSiteId = this.value;
        if (sourceSiteId !== '') {
            loadCategoryTree(sourceSiteId,fromTreePanel);
        }
    };


    //加载栏目树
   function loadCategoryTree(siteId, panel) {
        fromNode = {};
        sourceSiteId = siteId;
        reqTree(siteId, 'false',function(json) {
            jr.tree.load(panel, json, '/public/assets/tree/', function(a, d) {
                treeChecked(true,a,d);
            });
        });
    }

    function loadToCategoryTree() {
        reqTree(siteId, 'true',function(json) {
            jr.tree.load(toTreePanel, json, '/public/assets/tree/', function(a, d) {
                treeChecked(false,a,d);
            });
        });
    }

    function reqTree(siteId,withRoot, callback) {
        jr.xhr.request({
            uri: '?module=ajax&action=CategoryNodes&root='+withRoot+'&site_id='+siteId,
            method: 'GET',
            data: 'json'
        }, {
            success: function (json) {
                callback(json);
            }, error: function () {
                alert('栏目加载失败!');
            }
        });
    }

    function treeChecked(from,a, d) {
        a.className = 'node node-selelected';
        var json = jr.toJson(d);
        var node = { id: json.cid, name: a.innerHTML };
        if (from) {
            fromNode = node;
        } else {
            toNode = node;
        }
        absMsg();
    }

    function absMsg() {
        if (fromNode.id == undefined) {
            tipMsg.innerHTML = '源栏目未选择!';
        }else if (toNode.id == undefined) {
            tipMsg.innerHTML = '目标上级栏目未选择';
        } else {
            tipMsg.innerHTML = jr.template('即将从<b>{fName}</b>拷贝到<b>{tName}</b>', {
                fName: fromNode.name,
                tName: toNode.name
            });
        }
    }


    //重新加载栏目树
    function _reloadTree() {
        if (parent._loadCategoryTree) parent._loadCategoryTree();
        if (window._thisLoadCategoryTree) window._thisLoadCategoryTree();
    }

    loadToCategoryTree();

     jr.$('btn_clone').onclick = function() {
        if (toNode.id != undefined && fromNode.id != undefined) {
            var data = {
                sourceSiteId: sourceSiteId,
                targetSiteId: siteId,
                fromCid: fromNode.id,
                toCid: toNode.id,
                includeChild: jr.$('ck_with_child').checked,
                includeExtend: jr.$('ck_with_extend').checked,
                includeTemplateBind: jr.$('ck_with_templateBind').checked
            };
            jr.xhr.jsonPost(location.href, data,function(j) {
                if (j.result) {
                    showMsg2('操作成功', function() {
                        loadToCategoryTree();
                    });
                } else {
                    showErr(j.message);
                }
            });
        }
    };

</script>

</body>
</html>
